// WaveLoadingUIShader.shader
Shader "UI/WaveLoadingEffect"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        
        _WaveColor ("Wave Color", Color) = (0.2, 0.6, 1.0, 1.0)
        _BackgroundColor ("Background Color", Color) = (0.1, 0.1, 0.1, 1.0)
        _HighlightColor ("Highlight Color", Color) = (1, 1, 1, 1)
        _HighlightIntensity ("Highlight Intensity", Range(0, 1)) = 0.3
        _WaveSpeed ("Wave Speed", Range(0, 10)) = 2.0
        _WaveFrequency ("Wave Frequency", Range(0, 20)) = 5.0
        _WaveAmplitude ("Wave Amplitude", Range(0, 0.5)) = 0.1
        _FillAmount ("Fill Amount", Range(0, 1)) = 0.5
        _CircleRadius ("Circle Radius", Range(0, 1)) = 0.4
        _Smoothness ("Edge Smoothness", Range(0, 0.2)) = 0.02
        
        _StencilComp ("Stencil Comparison", Float) = 8
        _Stencil ("Stencil ID", Float) = 0
        _StencilOp ("Stencil Operation", Float) = 0
        _StencilWriteMask ("Stencil Write Mask", Float) = 255
        _StencilReadMask ("Stencil Read Mask", Float) = 255

        _ColorMask ("Color Mask", Float) = 15

        [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        Stencil
        {
            Ref [_Stencil]
            Comp [_StencilComp]
            Pass [_StencilOp]
            ReadMask [_StencilReadMask]
            WriteMask [_StencilWriteMask]
        }

        Cull Off
        Lighting Off
        ZWrite Off
        ZTest [unity_GUIZTestMode]
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask [_ColorMask]

        Pass
        {
            Name "Default"
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #pragma target 2.0

            #include "UnityCG.cginc"
            #include "UnityUI.cginc"

            #pragma multi_compile __ UNITY_UI_CLIP_RECT
            #pragma multi_compile __ UNITY_UI_ALPHACLIP

            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                float2 texcoord  : TEXCOORD0;
                float4 worldPosition : TEXCOORD1;
                UNITY_VERTEX_OUTPUT_STEREO
            };

            sampler2D _MainTex;
            fixed4 _Color;
            fixed4 _TextureSampleAdd;
            float4 _ClipRect;
            float4 _MainTex_ST;
            
            float4 _WaveColor;
            float4 _BackgroundColor;
            float4 _HighlightColor;
            float _HighlightIntensity;
            float _WaveSpeed;
            float _WaveFrequency;
            float _WaveAmplitude;
            float _FillAmount;
            float _CircleRadius;
            float _Smoothness;

            v2f vert(appdata_t v)
            {
                v2f OUT;
                UNITY_SETUP_INSTANCE_ID(v);
                UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(OUT);
                OUT.worldPosition = v.vertex;
                OUT.vertex = UnityObjectToClipPos(OUT.worldPosition);

                OUT.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);

                OUT.color = v.color * _Color;
                return OUT;
            }

            // 计算波浪高度
            float calculateWave(float2 position, float time)
            {
                float wave1 = sin(position.x * _WaveFrequency + time * _WaveSpeed) * _WaveAmplitude;
                float wave2 = sin(position.x * _WaveFrequency * 1.5 + time * _WaveSpeed * 1.3 + 1.0) * _WaveAmplitude * 0.5;
                return wave1 + wave2;
            }

            fixed4 frag(v2f IN) : SV_Target
            {
                // 应用裁剪矩形
                #ifdef UNITY_UI_CLIP_RECT
                clip (UnityGet2DClipping(IN.worldPosition.xy, _ClipRect) - 0.001);
                #endif

                // Alpha裁剪
                #ifdef UNITY_UI_ALPHACLIP
                clip (IN.color.a - 0.001);
                #endif

                // 将UV坐标转换为中心为原点的坐标系
                float2 centeredUV = IN.texcoord - float2(0.5, 0.5);
                
                // 计算到中心的距离
                float distanceToCenter = length(centeredUV);
                
                // 圆形容器遮罩
                float circleMask = smoothstep(_CircleRadius, _CircleRadius - _Smoothness, distanceToCenter);
                
                // 如果在圆外，直接返回透明
                if (circleMask <= 0.0)
                    return fixed4(0, 0, 0, 0);
                
                // 计算波浪效果
                float time = _Time.y;
                float waveHeight = calculateWave(centeredUV, time);
                
                // 计算水面高度（基于填充量）
                float waterLevel = _FillAmount - 0.5;
                
                // 判断当前像素是否在水面以下
                float isUnderWater = step(centeredUV.y, waterLevel + waveHeight);
                
                // 创建水面以上的渐变过渡
                float waterSurface = smoothstep(waterLevel + waveHeight, waterLevel + waveHeight + _Smoothness, centeredUV.y);
                
                // 组合颜色
                fixed4 backgroundColor = _BackgroundColor * circleMask;
                fixed4 waveColor = _WaveColor;
                
                // 根据位置混合颜色
                fixed4 finalColor = lerp(waveColor, backgroundColor, isUnderWater);
                
                // 应用圆形容器遮罩和原始颜色
                finalColor *= IN.color;
                finalColor.a *= circleMask;
                
                // 添加水面高光效果
                if (abs(centeredUV.y - (waterLevel + waveHeight)) < _Smoothness * 2.0)
                {
                    finalColor.rgb += _HighlightColor.rgb * _HighlightIntensity * (1.0 - waterSurface);
                }
                
                return finalColor;
            }
        ENDCG
        }
    }
}
